package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PoStorereq;
import com.ruoyi.system.service.IBdMaterialService;
import com.ruoyi.system.service.IPoStorereqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TPurchaseMapper;
import com.ruoyi.system.domain.TPurchase;
import com.ruoyi.system.service.ITPurchaseService;

/**
 * 物资采购Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class TPurchaseServiceImpl implements ITPurchaseService 
{
    @Autowired
    private TPurchaseMapper tPurchaseMapper;
    @Autowired
    private IPoStorereqService poStorereqService;
    /**
     * 查询物资采购
     * 
     * @param id 物资采购主键
     * @return 物资采购
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public int selectTPurchaseById(String id)
    {
        TPurchase tPurchase = tPurchaseMapper.selectTPurchaseById(id);
        tPurchase.setReceiveStatus(2);
        return tPurchaseMapper.updateTPurchase(tPurchase);
    }

    /**
     * 查询物资采购列表
     * 
     * @param tPurchase 物资采购
     * @return 物资采购
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public List<TPurchase> selectTPurchaseList(TPurchase tPurchase)
    {
        List<TPurchase> tPurchases = tPurchaseMapper.selectTPurchaseList(tPurchase);
        for (TPurchase purchase : tPurchases) {
            PoStorereq poStorereq = poStorereqService.selectPoStorereqByPkStorereq(purchase.getId());
            purchase.setReceiveStatus(poStorereq.getFbillstatus());
        }
        return tPurchases;
    }

    /**
     * 新增物资采购
     * 
     * @param tPurchase 物资采购
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public int insertTPurchase(TPurchase tPurchase)
    {
        tPurchase.setCreateTime(DateUtils.getNowDate());
        return tPurchaseMapper.insertTPurchase(tPurchase);
    }

    /**
     * 修改物资采购
     * 
     * @param tPurchase 物资采购
     * @return 结果
     */
    @Override
    public int updateTPurchase(TPurchase tPurchase)
    {
        return tPurchaseMapper.updateTPurchase(tPurchase);
    }

    /**
     * 批量删除物资采购
     * 
     * @param ids 需要删除的物资采购主键
     * @return 结果
     */
    @Override
    public int deleteTPurchaseByIds(String[] ids)
    {
        return tPurchaseMapper.deleteTPurchaseByIds(ids);
    }

    /**
     * 删除物资采购信息
     * 
     * @param id 物资采购主键
     * @return 结果
     */
    @Override
    public int deleteTPurchaseById(String id)
    {
        return tPurchaseMapper.deleteTPurchaseById(id);
    }

    @Override
    @DataSource(value = DataSourceType.MASTER)
    public BigDecimal getDisburseTotal(String pkProjectclass) {
        return tPurchaseMapper.getDisburseTotal(pkProjectclass);
    }
}
